package view;

import static org.junit.Assert.fail;

import java.util.zip.DataFormatException;

import javax.swing.table.JTableHeader;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import server.Rule;
import server.RulesTableType;
import server.Rule.permissionOpt;
import server.Rule.typeOpt;

public class RuleTableTest 
{
	/**
	 * Element for check
	 */
	private static RuleTable ruleTable = null;
	
	/**
	 * Rule id index in the table
	 */
	private static Integer ruleIndex = 1;
	
	/**
	 * Rule ip index in the table
	 */
	private static String ruleIP = "132.12.12.132";
	
	/**
	 * Rule port index in the table
	 */
	private static Integer rulePort = 80;
	
	/**
	 * Rule protocol index in the table
	 */
	private static String ruleProtocol = "HTTP";
	
	/**
	 * Rule permissions index in the table
	 */
	private static permissionOpt ruleAction = permissionOpt.DENY;
	
	/**
	 * Rule values
	 */
	private static String[] rule = 
		{ruleIndex.toString(), ruleIP, rulePort.toString(), ruleProtocol, ruleAction.toString()};
	
	/**
	 * Rows number in the table
	 */
	private static final int rowNumber = 1;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
		ruleTable = new RuleTable();
	}
	
	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
		ruleTable = null;
	}
	
	/**
	 * Checks right table values setting from the array of strings.
	 */
	@Test
	public void testSetRule()
	{
		ruleTable.setRule(rule);
		
		/* Check the right columns number */
		if(ruleTable.getColumnCount() != RuleTable.columnNames.length)
		{
			fail("Wrong table columns number");
		}
		
		/* Check the right rows number */		
		if(ruleTable.getRowCount() != rowNumber)
		{
			fail("Wrong table rows number");
		}
		
		/* Check the right id column index and value */
		String id = (String)ruleTable.getValueAt(rowNumber-1, RuleTable.idColumnIndex);
		if(!(id.equals(ruleIndex.toString())))
		{
			fail("Wrong rule index column number");
		}

		/* Check the right IP column index and value */
		String ip = (String)ruleTable.getValueAt(rowNumber-1, RuleTable.ipColumnIndex);
		if(!(ip.equals(ruleIP)))
		{
			fail("Wrong rule IP column number");
		}
		
		/* Check the right port column index and value */
		String port = (String)ruleTable.getValueAt(rowNumber-1, RuleTable.portColumnIndex);
		if(!(port.equals(rulePort.toString())))
		{
			fail("Wrong rule port column number");
		}
		
		/* Check the right protocol column index and value */
		String protocol = (String)ruleTable.getValueAt(rowNumber-1, RuleTable.protocolColumnIndex);
		if(!(protocol.equals(ruleProtocol)))
		{
			fail("Wrong rule protocol column number");
		}
		
		/* Check the right action column index and value */
		String action = (String)ruleTable.getValueAt(rowNumber-1, RuleTable.actionColumnIndex);
		if(!(action.equals(ruleAction.toString())))
		{
			fail("Wrong rule action column number");
		}
	}
	
	/**
	 * Checks right table header definition.
	 */
	@Test
	public void testGetRulesTableHeader()
	{
		JTableHeader ruleTableHeader  = ruleTable.getRulesTableHeader();
		
		/* Check the id column name */
		String idColumnHeader = 
			(String)ruleTableHeader.getColumnModel().getColumn(RuleTable.idColumnIndex).getHeaderValue();

		if(!(idColumnHeader.equals(RuleTable.columnNames[RuleTable.idColumnIndex])))
		{
			fail("Wrong id column name");
		}

		/* Check the ip column name */
		String ipColumnHeader = 
			(String)ruleTableHeader.getColumnModel().getColumn(RuleTable.ipColumnIndex).getHeaderValue();

		if(!(ipColumnHeader.equals(RuleTable.columnNames[RuleTable.ipColumnIndex])))
		{
			fail("Wrong IP column name");
		}
		
		/* Check the port column name */
		String portColumnHeader = 
			(String)ruleTableHeader.getColumnModel().getColumn(RuleTable.portColumnIndex).getHeaderValue();

		if(!(portColumnHeader.equals(RuleTable.columnNames[RuleTable.portColumnIndex])))
		{
			fail("Wrong port column name");
		}
		
		/* Check the protocol column name */
		String protocolColumnHeader = 
			(String)ruleTableHeader.getColumnModel().getColumn(RuleTable.protocolColumnIndex).getHeaderValue();

		if(!(protocolColumnHeader.equals(RuleTable.columnNames[RuleTable.protocolColumnIndex])))
		{
			fail("Wrong protocol column name");
		}
		
		/* Check the action column name */
		String actionColumnHeader = 
			(String)ruleTableHeader.getColumnModel().getColumn(RuleTable.actionColumnIndex).getHeaderValue();

		if(!(actionColumnHeader.equals(RuleTable.columnNames[RuleTable.actionColumnIndex])))
		{
			fail("Wrong action column name");
		}
	}
	
	/**
	 * Checks right table values for the context rule.
	 */
	@Test
	public void testGetRuleContext()
	{
		try
		{
			Rule rule = ruleTable.getRule(typeOpt.CONTEXT, RulesTableType.IN);
			
			/* Check the right id translation */
			if(rule.index != ruleIndex)
			{
				fail("Wrong index translation");				
			}
			
			/* Check the right IP translation for context rule */
			if(rule.iprange != null)
			{
				fail("Wrong IP translation");				
			}
			
			/* Check the right port translation */
			if(!rule.port.equals(rulePort))
			{
				fail("Wrong port translation");				
			}
			
			/* Check the right protocol translation */
			if(!rule.protocol.equals(ruleProtocol))
			{
				fail("Wrong protocol translation");				
			}
			
			/* Check the right action translation */
			if(rule.permission != ruleAction)
			{
				fail("Wrong action translation");				
			}
			
		} catch (DataFormatException e) {
			fail("Wrong checks in the function");
		}
	}
	
	/**
	 * Checks right table values for the IP rule.
	 */
	@Test
	public void testGetRuleIP()
	{
		try
		{
			Rule rule = ruleTable.getRule(typeOpt.IP, RulesTableType.IN);
			
			/* Check the right id translation */
			if(rule.index != ruleIndex)
			{
				fail("Wrong index translation");				
			}
			
			/* Check the right IP translation */
			if(!(rule.iprange.equals(ruleIP)))
			{
				fail("Wrong IP translation");				
			}
			
			/* Check the right port translation */
			if(!rule.port.equals(rulePort))
			{
				fail("Wrong port translation");				
			}
			
			/* Check the right protocol translation for IP rule */
			if(rule.protocol != null)
			{
				fail("Wrong protocol translation");				
			}
			
			/* Check the right action translation */
			if(rule.permission != ruleAction)
			{
				fail("Wrong action translation");				
			}
			
		} catch (DataFormatException e) {
			fail("Wrong checks in the function");
		}
	}
	
	/**
	 * Checks right table column disabling.
	 */
	@Test
	public void testSetDisabled()
	{
		String disabledHeader = RuleTable.disabledColumnHeader;
		
		ruleTable.setDisabled(RuleTable.actionColumnIndex);
	
		/* Check action column disabling */
		String actionHeader = 
			(String)ruleTable.getColumnModel().getColumn(RuleTable.actionColumnIndex).getHeaderValue();
		
		if(!(disabledHeader.equals(actionHeader)))
		{
			fail("Wrong diasbled column header");
		}

		/* Check other header values */
		
		/* Port column */
		String portHeader = 
			(String)ruleTable.getColumnModel().getColumn(RuleTable.portColumnIndex).getHeaderValue();
		
		if(!(portHeader.equals(RuleTable.columnNames[RuleTable.portColumnIndex])))
		{
			fail("Wrong enabled port column header");
		}
		
		/* Id column */
		String idHeader = 
			(String)ruleTable.getColumnModel().getColumn(RuleTable.idColumnIndex).getHeaderValue();
		
		if(!(idHeader.equals(RuleTable.columnNames[RuleTable.idColumnIndex])))
		{
			fail("Wrong enabled id column header");
		}
		
		/* IP column */
		String ipHeader = 
			(String)ruleTable.getColumnModel().getColumn(RuleTable.ipColumnIndex).getHeaderValue();
		
		if(!(ipHeader.equals(RuleTable.columnNames[RuleTable.ipColumnIndex])))
		{
			fail("Wrong enabled IP column header");
		}
		
		/* Protocol column */
		String protocolHeader = 
			(String)ruleTable.getColumnModel().getColumn(RuleTable.protocolColumnIndex).getHeaderValue();
		
		if(!(protocolHeader.equals(RuleTable.columnNames[RuleTable.protocolColumnIndex])))
		{
			fail("Wrong enabled protocol column header");
		}
	}
}



























